package com.group4.Utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.group4.Utility.JSON.*;
import com.group4.Utility.JSON.Object;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;

public class SemanticallySimilarWordsFinder {

	public static ArrayList<String> findSimilarWords(String str){
		ArrayList<String> results = new ArrayList<String>();
		
		String url = "http://conceptnet5.media.mit.edu/data/5.2/c/en/"+str+"?limit=3";
		
		HttpClient client = new DefaultHttpClient();
		HttpGet post = new HttpGet(url);
		HttpResponse response;
		
		try {
			response = client.execute(post);
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			Gson gson = new Gson();
			com.group4.Utility.JSON.Object obj = gson.fromJson(result.toString(), Object.class);
			for(int i = 0; i < obj.edges.size(); i++){
				for(int j = 0; j < 3; j++){
					if(!obj.edges.get(i).text[j].equals(str) && !obj.edges.get(i).text[j].equals("")){
						results.add(obj.edges.get(i).text[j]);
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return results;
	}
}
